<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport"
	content="width=device-width,initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<c:import url="/includes/header.jsp"></c:import>
<title></title>
</head>
<body>
	<div class="container">
		<div class="row">
			<div class="col-md-4"></div>
			<div class="col-md-4">
				<form class="form-horizontal" method="post" id="form">
					<input type="hidden" name="id" value="${admin.id }" /> <input
						type="hidden" name="roleCode" value="0" /> <input type="hidden"
						name="status" value="0" /> <input type="hidden" name="roleIds"
						value="" id="roleIds" />
					<div class="form-group">
						<label class="col-sm-3 control-label" for="content">账号:</label>
						<div class="col-sm-9">
							<input class="form-control" type="text" id="username"
								name="username" placeholder="输入用户名" value="${admin.username }">
						</div>
					</div>
					<div class="form-group">
						<label class="col-sm-3 control-label" for="content">密码:</label>
						<div class="col-sm-9">
							<input class="form-control" type="text" id="password"
								name="pwd">
						</div>
					</div>
					<div class="form-group">
						<label class="col-sm-3 control-label" for="content">邮箱:</label>
						<div class="col-sm-9">
							<input class="form-control" type="text" id="email" name="email"
								value="${admin.email }" placeholder="输入电子邮箱">
						</div>
					</div>
					<div class="form-group">
						<label class="col-sm-3 control-label" for="content">角色:</label>
						<div class="col-sm-9">
							<div style="overflow: auto;">
								<c:forEach items="${roles }" var="a">
									<input type="checkbox" name="roleId" value="${a.id }"
										<c:if test="${a.checked==true }"> checked="checked"</c:if>
										style="margin-top: 10px">${a.name }
							</c:forEach>
							</div>
						</div>
					</div>
					<div class="col-md-4"></div>
					<div class="col-md-4" style="margin-left: 50%;">
						<button type="button" class="btn btn-primary btn-sm">确定</button>
					</div>
					<div class="col-md-4"></div>
				</form>
			</div>
			<div class="col-md-4"></div>
		</div>
	</div>
</body>
</html>

<script type="text/javascript">
	$(function() {
		var validator = $("#form").validate({
			rules : {
				username : {
					required : true
				},
				email : {
					required : true,
					email : true
				}

			},
			messages : {
				username : {
					required : "账号必须填写"
				},
				email : {
					required : "电子邮箱必须填写",
					email : "电子邮箱不合法"
				}
			}
		});

		$(".btn")
				.click(
						function() {
							var formState = validator.form();
							var chk_value = new Array();
							$('input[name="roleId"]:checked').each(function() {
								chk_value.push($(this).val());
							});
							$("#roleIds").val(chk_value.join());
							if (formState) {
								var href = "${pageContext.request.contextPath}/admin/setting/getAdmin.do";
								$
										.ajax({
											url : "${pageContext.request.contextPath}/admin/setting/updateUser.do",
											type : "post",
											dataType : "json",
											data : $("#form").serialize(),
											async : true,
											success : function(data) {
												if (data.code == "0") {
													bootbox
															.alert(
																	data.data,
																	function() {
																		window.location = href;
																	});

												} else {
													bootbox.alert("网络不稳定,添加失败");
												}
											},
											error : function() {
												bootbox.alert("网络不稳定,添加失败");
											}
										});
							}
						});

	});
</script>